runtime.loadDex("./classes3.dex");

importClass(android.content.Context);
importClass(android.graphics.Bitmap);
importClass(android.graphics.BitmapFactory);
importClass(android.graphics.Point);
importClass(android.widget.TextView);
importClass(com.baidu.ai.edge.core.infer.InferConfig);
importClass(com.baidu.ai.edge.core.infer.InferManager);
importClass(com.baidu.ai.edge.core.ocr.OcrResultModel);
importClass(java.io.InputStream);
importClass(java.util.List);
importClass(java.io.FileInputStream);

/**
 * 通用arm 开源OCR模型
 */

let CONFIDENCE = 0.3;

/* 1. 准备配置类，初始化Manager类。可以在onCreate或onResume中触发，请在非UI线程里调用 */
let config = new InferConfig(context.getAssets(), "infer-ocr/config.json");
let manager = new InferManager(context, config, "");

events.on("exit", function () {
  manager.destroy();
});

module.exports = function (filePath, img) {
  let resStrArr = [];

  if (filePath) {
    /* 2.1 准备图片，作为Bitmap输入 */
    log("文件是否存在 = " + files.exists(filePath));
    filePath = java.lang.String(filePath);
    let bitmap = BitmapFactory.decodeStream(new FileInputStream(filePath));

    /* 2.2 推理图片及解析结果 */
    let results = null;

    // 在模型销毁前可以不断调用。但是不支持多线程
    results = manager.ocr(bitmap, CONFIDENCE);

    // 解析结果
    if (results != null) {
      if (results.size() > 0) {
        let mResults = [];
        var len = results.size();
        for (var i = 0; i < len; i++) {
          let result = results.get(i);
          let labelName = result.getLabel();
          let points = result.getPoints();
          let rect = [];
          var pSize = points.size();
          for (var j = 0; j < pSize; j++) {
            let point = points.get(j);
            rect.push([point.x, point.y]);
          }
          mResults.push({
            labelName: labelName,
            rect: rect,
          });
        }
        resStrArr = mResults;
        mResults = null;
      }
    }

    /* 3. 销毁模型。可以在onDestroy或onPause中触发，请在非UI线程里调用 */

    bitmap.recycle();
  } else if (img) {
    /* 2.1 准备图片，作为Bitmap输入 */
    let bitmap = img.bitmap;
    /* 2.2 推理图片及解析结果 */
    let results = null;

    // 在模型销毁前可以不断调用。但是不支持多线程
    results = manager.ocr(bitmap, CONFIDENCE);

    // 解析结果
    if (results != null) {
      if (results.size() > 0) {
        let mResults = [];
        var len = results.size();
        for (var i = 0; i < len; i++) {
          let result = results.get(i);
          let labelName = result.getLabel();
          let points = result.getPoints();
          let rect = [];
          var pSize = points.size();
          for (var j = 0; j < pSize; j++) {
            let point = points.get(j);
            rect.push([point.x, point.y]);
          }
          mResults.push({
            labelName: labelName,
            rect: rect,
          });
        }
        resStrArr = mResults;
        mResults = null;
      }
    }

    /* 3. 销毁模型。可以在onDestroy或onPause中触发，请在非UI线程里调用 */

    bitmap.recycle();
    img.recycle();
  }
  log("Finish running\n");
  log("resStrArr =");
  log(JSON.stringify(resStrArr, null, 2));
  return resStrArr;
};
